/* @(#)FmmOpenHelper.java
 ** 
 ** Copyright (C) 2012 by Steven D. Stamps
 **
 **             Trademarks & Copyrights
 ** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
 ** and FlywheelMS(TM) are exclusive trademarks of Steven D. Stamps
 ** and may only be used freely for the purpose of identifying the
 ** unforked version of this software.  Subsequent forks (if any) may
 ** not use these trademarks.  All other rights are reserved.
 **
 ** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
 ** are also exclusive trademarks of Steven D. Stamps.  These may be used
 ** freely within the unforked FlywheelMS application and documentation.
 ** All other rights are reserved.
 **
 ** Trademark information is available at
 ** <http://www.flywheelms.com/trademarks>
 **
 ** Flywheel Management Science(TM) is a copyrighted body of management
 ** metaphors, governance processes, and leadership techniques that is
 ** owned by Steven D. Stamps.  These copyrighted materials may be freely
 ** used, without alteration, by the community (users and developers)
 ** surrounding this GPL3-licensed software.  Additional copyright
 ** information is available at <http://www.flywheelms.org/copyrights>
 **
 **              GPL3 Software License
 ** This program is free software: you can use it, redistribute it and/or
 ** modify it under the terms of the GNU General Public License, version 3,
 ** as published by the Free Software Foundation. This program is distributed
 ** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 ** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 ** PURPOSE.  See the GNU General Public License for more details. You should
 ** have received a copy of the GNU General Public License, in a file named
 ** COPYING, along with this program.  If you cannot find your copy, see
 ** <http://www.gnu.org/licenses/gpl-3.0.html>.
 */
package com.flywheelms.library.fmm.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.flywheelms.library.fmm.database.sqlite.FmmLocalDatabaseBuilder;
import com.flywheelms.library.fmm.database.sqlite.LocalDatabaseRowLoader;

public class FmmOpenHelper extends SQLiteOpenHelper {
	
	private static FmmOpenHelper singleton;
	
	public static final String DATABASE_NAME = "fmm.db";
	public static final int DATABASE_VERSION = 1;
	
	public static final boolean START_WITH_NEW_DATABASE = true;
	public int getWritableDatabaseCount = 0;
	
	public boolean newDatabase = false;

	public FmmOpenHelper(Context aContext, String aDatabaseName, CursorFactory aCursorFactory,
			int aVersion) {
		super(aContext, aDatabaseName, aCursorFactory, aVersion);
	}
	
	public static FmmOpenHelper getInstance() {
		if (singleton == null) {
			singleton = new FmmOpenHelper(
				FmmHelper.getContext(),
				FmmOpenHelper.DATABASE_NAME,
				null,
				DATABASE_VERSION);
		}
		return singleton;
	}

	@Override
	public void onCreate(SQLiteDatabase aDatabase) {
		this.newDatabase = true;
		FmmLocalDatabaseBuilder.createDatabase(aDatabase);
		LocalDatabaseRowLoader.insertRows(aDatabase, "Demo-1");
	}

	@Override
	public void onUpgrade(SQLiteDatabase aDatabase, int arg1, int arg2) {
		// TODO Auto-generated method stub
	}
	
    @Override
    public synchronized SQLiteDatabase getWritableDatabase() {
    	if(START_WITH_NEW_DATABASE && getWritableDatabaseCount == 0) {
    		close();
    		FmmHelper.getContext().getDatabasePath(getDatabaseName()).delete();  // development hack
    	}
    	++getWritableDatabaseCount;
    	return super.getWritableDatabase();
    }

}
